package com.zhanglijie.theory.guardsuspension;

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author zhanglijie
 * @version 1.0
 * @since 1.1.0 2022/8/11 0011 1:11
 */
public class RequestQueue {
     private Queue<Request> queue = new LinkedList<>();

    /**
     * 生产者执行
     * @param request
     *
     */
    public synchronized   void putRequest(Request request) throws InterruptedException {
        queue.offer(request);
        notifyAll();
    }

    /**
     * 消费者执行
     *
     */
    public synchronized   Request getRequest() throws InterruptedException {
       // 体现保护性暂挂
        while (queue.isEmpty()){
            try {
                wait();
            }catch (Exception e){}

        }
        return queue.remove();
    }
}
